package me.boot.auth.spring;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import me.boot.auth.spring.model.SpringUser;
import me.boot.sys.entity.SysUser;
import me.boot.web.common.template.model.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * File Name             :  AuthUtils
 *
 * @author :  sylar
 * @create :  2018/11/17
 * Description           :
 * Reviewed By           :
 * Reviewed On           :
 * Version History       :
 * Modified By           :
 * Modified Date         :
 * Comments              :
 * CopyRight             : COPYRIGHT(c) me.iot.com   All Rights Reserved
 * *******************************************************************************************
 */
@Slf4j
@UtilityClass
public class AuthUtils {

    public SpringUser fromSysUser(SysUser user) {
        SpringUser foo = new SpringUser();
        BeanUtil.copyProperties(user, foo, true);
        foo.setEnabled(!user.getInvalid());
        return foo;
    }

    public void checkAuth() {
        Preconditions.checkNotNull(getLoginUser(), "未登录");
    }

    public Long getLoginUserId() {
        SysUser user = getLoginUser();
        return user != null ? user.getId() : null;
    }

    public SysUser getLoginUser() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx != null) {
            Authentication auth = ctx.getAuthentication();
            if (auth != null && auth.getPrincipal() != null) {
                if (auth.getPrincipal() instanceof SysUser) {
                    return (SysUser) auth.getPrincipal();
                }
            }
        }
        return null;
    }

    public String trimResourceUrl(String url) {
        if (Strings.isNullOrEmpty(url)) {
            return url;
        }
        String slash = "/";
        url = url.replace("*", "");
        while (url.endsWith(slash)) {
            int index = url.lastIndexOf(slash);
            if (index > 0) {
                url = url.substring(0, index);
            }
        }
        return url;
    }

    public void output(HttpServletResponse response, Result<?> result) {
        output(response, result, HttpServletResponse.SC_OK);
    }

    public void output(HttpServletResponse response, Result<?> result, int httpStatus) {
        log.info(result.toString());
        PrintWriter writer = null;
        try {
            response.setStatus(httpStatus);
            response.setContentType(AuthConst.CONTENT_TYPE);
            writer = response.getWriter();
            writer.write(result.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }
}
